<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="处方信息id" prop="prescriptionInfoId">
        <el-input
          v-model="queryParams.prescriptionInfoId"
          placeholder="请输入处方信息id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处方类型" prop="prescriptionType">
        <el-select v-model="queryParams.prescriptionType" placeholder="请选择处方类型" clearable>
          <el-option
            v-for="dict in medicine_ptype"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="药品 id" prop="medicineId">
        <el-input
          v-model="queryParams.medicineId"
          placeholder="请输入药品 id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组号" prop="groupNumber">
        <el-input
          v-model="queryParams.groupNumber"
          placeholder="请输入组号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单次用量" prop="singleDose">
        <el-input
          v-model="queryParams.singleDose"
          placeholder="请输入单次用量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用法" prop="usage">
        <el-input
          v-model="queryParams.usage"
          placeholder="请输入用法"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="天数" prop="days">
        <el-input
          v-model="queryParams.days"
          placeholder="请输入天数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总量" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入总量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="就诊记录id" prop="visitRecordId">
        <el-input
          v-model="queryParams.visitRecordId"
          placeholder="请输入就诊记录id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="治疗方案id" prop="cureId">
        <el-input
          v-model="queryParams.cureId"
          placeholder="请输入治疗方案id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段2" prop="ext2">
        <el-input
          v-model="queryParams.ext2"
          placeholder="请输入扩展字段2"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段3" prop="ext3">
        <el-input
          v-model="queryParams.ext3"
          placeholder="请输入扩展字段3"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段4" prop="ext4">
        <el-input
          v-model="queryParams.ext4"
          placeholder="请输入扩展字段4"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段5" prop="ext5">
        <el-input
          v-model="queryParams.ext5"
          placeholder="请输入扩展字段5"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['sn:prescriptionInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sn:prescriptionInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sn:prescriptionInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['sn:prescriptionInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prescriptionInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="处方信息id" align="center" prop="prescriptionInfoId" />
      <el-table-column label="处方类型" align="center" prop="prescriptionType">
        <template #default="scope">
          <dict-tag :options="medicine_ptype" :value="scope.row.prescriptionType"/>
        </template>
      </el-table-column>
      <el-table-column label="药品 id" align="center" prop="medicineId" />
      <el-table-column label="组号" align="center" prop="groupNumber" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="单次用量" align="center" prop="singleDose" />
      <el-table-column label="用法" align="center" prop="usage" />
      <el-table-column label="天数" align="center" prop="days" />
      <el-table-column label="总量" align="center" prop="totalAmount" />
      <el-table-column label="就诊记录id" align="center" prop="visitRecordId" />
      <el-table-column label="治疗方案id" align="center" prop="cureId" />
      <el-table-column label="扩展字段2" align="center" prop="ext2" />
      <el-table-column label="扩展字段3" align="center" prop="ext3" />
      <el-table-column label="扩展字段4" align="center" prop="ext4" />
      <el-table-column label="扩展字段5" align="center" prop="ext5" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['sn:prescriptionInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['sn:prescriptionInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改患者处方信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="prescriptionInfoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="处方类型" prop="prescriptionType">
          <el-select v-model="form.prescriptionType" placeholder="请选择处方类型">
            <el-option
              v-for="dict in medicine_ptype"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="药品 id" prop="medicineId">
          <el-input v-model="form.medicineId" placeholder="请输入药品 id" />
        </el-form-item>
        <el-form-item label="组号" prop="groupNumber">
          <el-input v-model="form.groupNumber" placeholder="请输入组号" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="单次用量" prop="singleDose">
          <el-input v-model="form.singleDose" placeholder="请输入单次用量" />
        </el-form-item>
        <el-form-item label="用法" prop="usage">
          <el-input v-model="form.usage" placeholder="请输入用法" />
        </el-form-item>
        <el-form-item label="天数" prop="days">
          <el-input v-model="form.days" placeholder="请输入天数" />
        </el-form-item>
        <el-form-item label="总量" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总量" />
        </el-form-item>
        <el-form-item label="就诊记录id" prop="visitRecordId">
          <el-input v-model="form.visitRecordId" placeholder="请输入就诊记录id" />
        </el-form-item>
        <el-form-item label="治疗方案id" prop="cureId">
          <el-input v-model="form.cureId" placeholder="请输入治疗方案id" />
        </el-form-item>
        <el-form-item label="扩展字段2" prop="ext2">
          <el-input v-model="form.ext2" placeholder="请输入扩展字段2" />
        </el-form-item>
        <el-form-item label="扩展字段3" prop="ext3">
          <el-input v-model="form.ext3" placeholder="请输入扩展字段3" />
        </el-form-item>
        <el-form-item label="扩展字段4" prop="ext4">
          <el-input v-model="form.ext4" placeholder="请输入扩展字段4" />
        </el-form-item>
        <el-form-item label="扩展字段5" prop="ext5">
          <el-input v-model="form.ext5" placeholder="请输入扩展字段5" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PrescriptionInfo">
import { listPrescriptionInfo, getPrescriptionInfo, delPrescriptionInfo, addPrescriptionInfo, updatePrescriptionInfo } from "@/api/sn/prescriptionInfo";

const { proxy } = getCurrentInstance();
const { medicine_ptype } = proxy.useDict('medicine_ptype');

const prescriptionInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    prescriptionInfoId: null,
    prescriptionType: null,
    medicineId: null,
    groupNumber: null,
    name: null,
    singleDose: null,
    usage: null,
    days: null,
    totalAmount: null,
    visitRecordId: null,
    cureId: null,
    ext2: null,
    ext3: null,
    ext4: null,
    ext5: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询患者处方信息列表 */
function getList() {
  loading.value = true;
  listPrescriptionInfo(queryParams.value).then(response => {
    prescriptionInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    prescriptionInfoId: null,
    prescriptionType: null,
    medicineId: null,
    groupNumber: null,
    name: null,
    singleDose: null,
    usage: null,
    days: null,
    totalAmount: null,
    visitRecordId: null,
    cureId: null,
    ext2: null,
    ext3: null,
    ext4: null,
    ext5: null
  };
  proxy.resetForm("prescriptionInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.prescriptionInfoId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加患者处方信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _prescriptionInfoId = row.prescriptionInfoId || ids.value
  getPrescriptionInfo(_prescriptionInfoId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改患者处方信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["prescriptionInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.prescriptionInfoId != null) {
        updatePrescriptionInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPrescriptionInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _prescriptionInfoIds = row.prescriptionInfoId || ids.value;
  proxy.$modal.confirm('是否确认删除患者处方信息编号为"' + _prescriptionInfoIds + '"的数据项？').then(function() {
    return delPrescriptionInfo(_prescriptionInfoIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('sn/prescriptionInfo/export', {
    ...queryParams.value
  }, `prescriptionInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
